package com.newtype.meeting.client.widget.backend;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.model.Title;

/**
 * 职称管理面板控件
 * @author 汪恭泽
 * May 31, 2011 10:26:49 AM
 */
public class TitleManagementPanel extends ContentPanel {
	//职称Grid数据源
	private GroupingStore<Title> store = new GroupingStore<Title>();

	public TitleManagementPanel() {
		setHeading("职称管理");
		setLayout(new FitLayout());
		setIconStyle("icon-system");

		RowNumberer r = new RowNumberer();

		store.groupBy("degree");
		//标题列
		ColumnConfig caption = new ColumnConfig("titleCaption", "职称", 100);
		ColumnConfig degree = new ColumnConfig("degree", "职级", 100);
		//定义列的定义
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		configs.add(r);
		configs.add(caption);
		configs.add(degree);
		//定义列模型
		final ColumnModel cm = new ColumnModel(configs);

		GroupingView view = new GroupingView();
		view.setShowGroupedColumn(false);
		view.setForceFit(true);
		view.setGroupRenderer(new GridGroupRenderer() {
			@Override
			public String render(GroupColumnData data) {
				String f = cm.getColumnById(data.field).getHeader();
				String l = data.models.size() == 1 ? "Item" : "Items";
				return f + ": " + data.group + " (" + data.models.size() + " "
						+ l + ")";
			}
		});
		//构造职称Grid
		Grid<Title> grid = new Grid<Title>(store, cm);
		grid.setView(view);
		grid.setBorders(true);
		grid.addPlugin(r);
		grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<Title>>() {
					public void handleEvent(SelectionChangedEvent<Title> be) {
						if (be.getSelection().size() > 0) {
							String id = be.getSelection().get(0).getId();
							Dispatcher.forwardEvent(
									AppEvents.ToTitleEditWindow, id);

						}
					}
				});
		add(grid);
		//定义工具栏
		ToolBar toolBar = new ToolBar();
		Button btnAdd = new Button("新增");
		btnAdd.setIconStyle("icon-add");
		btnAdd.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				Window window = new TitleAddWindow();
				window.show();
			}
		});
		toolBar.add(btnAdd);
		setTopComponent(toolBar);
	}

	public GroupingStore<Title> getStore() {
		return store;
	}
}
